﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ChatForum.DataContract;
using ChatForum.SOAP.ServiceContract;
using System.ServiceModel;
using ChatForum.Domain;
using ChatForum.Messaging;

namespace ChatForum.Service.SOAP
{
	public class AccountService : ServiceBase, IAccountService
	{
		private SmtpServerBase smtpServer;
		public AccountService(BusinessLogic logic, SmtpServerBase smtpServer)
			: base(logic)
		{
			this.smtpServer = smtpServer;
		}

		public void BlockUser(UserDTO user)
		{
			try
			{
				logic.BlockUser(user.Convert());

				if (smtpServer != null)
				{
					string messageBody = string.Format("Hi {0}\n\nYour account got disabled. Please reply to this mail thread for the reason.\n\nThanks & Regards\nAdmin.", user.UserId);
					string subject = "Your account has been disabled";
					smtpServer.SendAsynchMessage(null, Enumerable.Repeat<string>(user.EmailId, 1), messageBody, subject, Enumerable.Repeat<string>(user.AlternateEmailId, 1), null);
				}
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public void ChangePassword(string userId, string oldPassword, string password)
		{
			try
			{
				logic.ChangeMyPassword(userId, oldPassword, password);
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public IEnumerable<UserDTO> GetAllUser()
		{
			try
			{
				return logic.GetAllUser().Select(u => new UserDTO(u));
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public UserDTO GetUser(string userId)
		{
			try
			{
				return new UserDTO(logic.GetUser(userId));
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public void Login(string userId, string password)
		{
			try
			{
				logic.LogIn(userId, password);
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public void Logout(string userId)
		{
			try
			{
				logic.Logout(userId);
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public void ModifyAccountDetails(UserDTO user)
		{
			try
			{
				logic.ModifyAccount(user.Convert());
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public void RegisterUser(UserDTO user)
		{
			try
			{
				logic.RegisterMe(user.Convert());
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}

		public void UnblockUser(UserDTO user)
		{
			try
			{
				logic.UnblockUser(user.Convert());

				if (smtpServer != null)
				{
					string messageBody = string.Format("Hi {0}\n\nYour account got enabled. Please enjoy the forum service.\n\nThanks & Regards\nAdmin.", user.UserId);
					string subject = "Your account has been enabled";
					smtpServer.SendAsynchMessage(null, Enumerable.Repeat<string>(user.EmailId, 1), messageBody, subject, Enumerable.Repeat<string>(user.AlternateEmailId, 1), null);
				}
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}


		public void SendMessage(UserDTO user, string subject, string body)
		{
			try
			{
				if (smtpServer == null)
					throw new ArgumentException("SMTP Server not configured.");
				smtpServer.SendMessage(null, Enumerable.Repeat<string>(user.EmailId, 1), body, subject, Enumerable.Repeat<string>(user.AlternateEmailId, 1), null);
			}
			catch (Exception ex)
			{
				throw new FaultException<ChatForumFault>(new ChatForumFault() { FaultMessage = ex.Message });
			}
		}
	}
}
